<script>
  total_order = <?php echo $count_order; ?>;
  
  function load_order()
  {
    before = total_order;
    new_order = total_order;
    $("#order_1").clone().appendTo("#order").attr("id","order_"+new_order);

    $("#order_"+new_order+" #form1").attr("id","form"+new_order);
    $("#order_"+new_order+" #form"+new_order).html("Order Form #"+new_order);

    $("#order_"+new_order+" #code1").attr("id","code"+new_order).val('');
    $("#order_"+new_order+" #code"+new_order).attr("name","code"+new_order);

    $("#order_"+new_order+" #quantity1").attr("id","quantity"+new_order).val('');
    $("#order_"+new_order+" #quantity"+new_order).attr("name","quantity"+new_order);

    $("#order_"+new_order+" #description1").attr("id","description"+new_order);
    $("#order_"+new_order+" #description"+new_order).attr("name","description"+new_order);

    $("#order_"+before+" .rfloat").empty();

    $("#order_"+new_order+" .rfloat").html('<span id="add_order"><a href="javascript:add_order();" id="add">Add Order</a> | </span><a href="javascript:delete_order('+total_order+');">Delete Order</a>');

    var tmp = parseInt($("#count_order").val());
    $("#count_order").val(tmp+1);
  }

  function add_order()
  {
    before = total_order;
    total_order = total_order + 1;
    $("#order_1").clone().appendTo("#order").attr("id","order_"+total_order);

    $("#order_"+total_order+" #form1").attr("id","form"+total_order);
    $("#order_"+total_order+" #form"+total_order).html("Order Form #"+total_order);

    $("#order_"+total_order+" #code1").attr("id","code"+total_order).val('');
    $("#order_"+total_order+" #code"+total_order).attr("name","code"+total_order);

    $("#order_"+total_order+" #quantity1").attr("id","quantity"+total_order).val('');
    $("#order_"+total_order+" #quantity"+total_order).attr("name","quantity"+total_order);

    $("#order_"+total_order+" #description1").attr("id","description"+total_order);
    $("#order_"+total_order+" #description"+total_order).attr("name","description"+total_order);

    $("#order_"+before+" .rfloat").empty();

    $("#order_"+total_order+" .rfloat").html('<span id="add_order"><a href="javascript:add_order();" id="add">Add Order</a> | </span><a href="javascript:delete_order('+total_order+');">Delete Order</a>');

    var tmp = parseInt($("#count_order").val());
    $("#count_order").val(tmp+1);
  }

  function delete_order(id)
  {
    if(total_order>1){
      var tmp =parseInt( $("#count_order").val());
      $("#count_order").val(tmp-1);

      total_order = total_order - 1;
      $("#order_"+id).remove();

      if(total_order > 1)
        $("#order_"+total_order+" .rfloat").html('<span id="add_order"><a href="javascript:add_order();" id="add">Add Order</a> | </span><a href="javascript:delete_order('+total_order+');">Delete Order</a>');
      else
        $("#order_"+total_order+" .rfloat").html('<span id="add_order"><a href="javascript:add_order();" id="add">Add Order</a></span>');
    }
  }

  $(document).ready(function(){
    $("#count_order").val(total_order);

    for(var a=1;a<total_order;a++)
    {
      load_order();
    }
  });
</script>

<div class='mainContainer home'> <!-- Main div for the contain, behaves like a table-->
	<div><!-- Direct child of mainContainer, behaves like a table cell-->
    <a title="Download Order Form - Excel Version" style="float:right; margin-right:20px;margin-top:5px;" href="<?php echo bu('assets/catalogs/order_form.xls'); ?>"><img src="<?php echo bu('images/icons/xls-icon.gif'); ?>" style="width:20px;" /></a>
		<h2 class='differentAlign'>Order <span class='differentHeading'>Form</span></h2>
		<div class='spacer'>&nbsp;</div>

    <?php echo $this->renderPartial('/partials/_notifications'); ?>
    <?php
      $form = $this->beginWidget("CActiveForm",array(
        "action" => bu("orders/create"),
      ));
    ?>

      <div id="order">
        <div class="message info">
          <h3>Required Info</h3>
        </div>
        <?php echo $form->errorSummary($detail); ?>
        <?php echo $form->errorSummary($order); ?>
        <p>
          <label>Name *</label>
          <?php echo $form->textField($order, "name"); ?>
          <?php echo $form->error($order, "name"); ?>
        </p>
        <p>
          <label>Email *</label>
          <?php echo $form->textField($order, "email"); ?>
          <?php echo $form->error($order, "email"); ?>
        </p>
        <p>
          <label>Address *</label>
          <?php echo $form->textField($order, "address"); ?>
          <?php echo $form->error($order, "address"); ?>
        </p>
        <p>
          <label>Phone / Mobile *</label>
          <?php echo $form->textField($order, "phone"); ?>
          <?php echo $form->error($order, "phone"); ?>
        </p>
        <br />

        <div id="order_1">
          <div class="message info">
            <h3><span id="form1">Order Form #1</span> <span class="rfloat"><span id="add_order"><a href="javascript:add_order();" id="add">Add Order</a></span></span></h3>
          </div>
          <p>
            <label>Code *</label>
            <?php echo $form->textField($detail, "code", array('id' => 'code1', 'name' => 'code1')); ?>
            <?php echo $form->error($detail, "code"); ?>
          </p>
          <p>
            <label>Quantity *</label>
            <?php echo $form->textField($detail, "quantity", array('id' => 'quantity1', 'class' => 'number', 'name' => 'quantity1')); ?>
            <?php echo $form->error($detail, "quantity"); ?>
          </p>
          <p>
            <label>Description</label>
            <?php echo $form->textArea($detail, "description", array('id' => 'description1', 'name' => 'description1')); ?>
            <?php echo $form->error($detail, "description"); ?>
          </p>

          <div class='spacer'>&nbsp;</div>
          <hr /><span class='footnote'>* Must not be blank</span>
          <div class='spacer'>&nbsp;</div>
        </div>
      </div>

      <input type="hidden" id="count_order" name="count_order" value=1 />
      <input type='submit' value='Submit' />
      <input type='submit' value='Reset' />
		<?php $this->endWidget(); ?>
	</div>
</div>

<div class='spacer'>&nbsp;</div>